package com.facebookproject.graph.getlist;

import static com.facebookproject.Const.APP_FB_ID;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.facebook.android.Facebook;
import com.facebook.android.FacebookError;
import com.facebook.android.Util;
import com.facebookproject.graph.Holder;
import com.facebookproject.graph.element.Elements;
import com.facebookproject.loginfb.LoginFacebook;
@SuppressWarnings("deprecation")
public class GetRestaurantNearby {
	private Facebook facebook = new Facebook(APP_FB_ID);
	
	public List<Elements> getrestaurantsnearby(Holder fbHolder,boolean b) {
		List<Elements> list = new ArrayList<Elements>();
		
		facebook = new LoginFacebook().login(fbHolder.context,facebook);
		try {
			String id=null,name=null,ele1=null,ele2=null,ele3=null,ele4=null;
			String q = "q=" + "restaurant";
			String type = "&type=" + "place";
			String fields = "&fields=id,name,location,likes,were_here_count,category";
			String center = "&center=" + "20.974026,105.762984";
			String distance = "&distance=" + "5000";
			String access_token = "&access_token="+facebook.getAccessToken();

			HttpGet get = new HttpGet("https://graph.facebook.com/search?"+q+fields+type+center+distance+access_token);
			HttpResponse response =new DefaultHttpClient().execute(get);
			String y = EntityUtils.toString(response.getEntity());
			JSONArray ja = Util.parseJson(y).getJSONArray("data");
			for (int i = 0; i < ja.length(); i++) {
				if(b)break;
				JSONObject tmp = (JSONObject) ja.get(i);
				id = tmp.getString("id");
				name = tmp.getString("name");
				JSONObject loc = tmp.getJSONObject("location");
				
				if(tmp.has("location")){
					ele1= "Address : ";
					if(loc.has("street")&&loc.getString("street")!="")ele1 = ele1 + loc.getString("street");
					if(loc.has("city")&&loc.getString("city")!="")ele1 = ele1 +","+loc.getString("city");
					if(loc.has("state")&&loc.getString("state")!="")ele1 = ele1 +","+ loc.getString("state");
					if(loc.has("country")&&loc.getString("country")!="")ele1 = ele1 +","+ loc.getString("country");
				}
				if(tmp.has("likes"))
					ele2 = tmp.getString("likes") + " people like this";
				if(tmp.has("were_here_count"))
					ele4 = tmp.getString("were_here_count")+ " people have been here";
					Elements ele = new Elements(id, name, ele1, ele2, ele3, ele4);
				list.add(ele);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (FacebookError e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}

}
